#ifndef ENTRANCEVIEWMODEL_HPP
#define ENTRANCEVIEWMODEL_HPP

#include <QObject>
#include <QWidget>
#include <QVariantMap>
#include "assembly/model/EntranceModel.hpp"
#include "assembly/view/chart/GanttChartView.hpp"
#include "assembly/view/chart/LadderLineChartView.hpp"

class EntranceViewModel : public QObject {
    Q_OBJECT

public:
    explicit EntranceViewModel(QObject *parent = nullptr)
        : QObject(parent), model(new EntranceModel(this)) {
        // 连接模型的信号到 ViewModel 的信号
        connect(model, &EntranceModel::chartTypeChanged, this, &EntranceViewModel::chartTypeChanged);
        connect(model, &EntranceModel::startTimeChanged, this, &EntranceViewModel::startTimeChanged);
        connect(model, &EntranceModel::endTimeChanged, this, &EntranceViewModel::endTimeChanged);
        connect(model, &EntranceModel::datesChanged, this, &EntranceViewModel::datesChanged);

        // 使用 invokeMethod 延迟发射初始状态信号，确保视图已连接
        QMetaObject::invokeMethod(this, "emitInitialState", Qt::QueuedConnection);
    }

    QStringList getDates() const {
        return model->getDates();
    }

    EntranceModel::ChartType getChartType() const {
        return model->getChartType();
    }

    void setChartType(EntranceModel::ChartType type) {
        model->setChartType(type);
        // 信号已经由模型通过连接转发
    }

    QDateTime getStartTime() const {
        return model->getStartTime();
    }

    QDateTime getEndTime() const {
        return model->getEndTime();
    }

    void setStartTime(const QString &dateString, const QTime &time) {
        QDate date = QDate::fromString(dateString, "yyyy-MM-dd");
        if (date.isValid()) {
            QDateTime startTime(date, time);
            model->setStartTime(startTime);
            Huben::instance().dispatch(ActionWrapper::ActionType::EditModify, createDispatchVariant({ {"StartTime", model->getStartTime().time().toString()} }));
        }
    }

    void setEndTime(const QString &dateString, const QTime &time) {
        QDate date = QDate::fromString(dateString, "yyyy-MM-dd");
        if (date.isValid()) {
            QDateTime endTime(date, time);
            model->setEndTime(endTime);
            Huben::instance().dispatch(ActionWrapper::ActionType::EditModify, createDispatchVariant({ {"EndTime", model->getEndTime().time().toString()} }));
        }
    }

    // 设置选中的日期并更新开始和结束时间
    void setSelectedDate(const QString &selectedDate) {
        // 调用 Model 的更新函数
        model->updateTimesForSelectedDate(selectedDate);
        // 通过 Huben 发送动作（根据具体业务逻辑，保留或移除）
        // Huben::instance().dispatch(ActionWrapper::ActionType::EditBasic, QVariant(QMap<QString, QVariant> {{"SelectDate", selecteDate}}));
        Huben::instance().dispatch(ActionWrapper::ActionType::EditModify, createDispatchVariant({{"SelectDate", selectedDate}}));
    }

    // 返回相应的图表视图（GanttChartView 或 ScatterPlotView）
    QWidget* createChartWidget(EntranceModel::ChartType type, QWidget* parent) {
        if (type == EntranceModel::ChartType::Rhythm) {
            return new GanttChartView(parent);    // Rhythm 时返回 GanttChartView
        } else {
            return new LadderLineChartView(parent);  // 其他情况返回 ScatterPlotView
        }
    }

signals:
    void chartTypeChanged(EntranceModel::ChartType type);
    void startTimeChanged(const QDateTime &startTime);
    void endTimeChanged(const QDateTime &endTime);
    void datesChanged(const QStringList &dates); // 如果需要

private slots:
    void emitInitialState() {
        emit chartTypeChanged(model->getChartType());
        emit startTimeChanged(model->getStartTime());
        emit endTimeChanged(model->getEndTime());
        emit datesChanged(model->getDates());
    }

private:
    QVariant createDispatchVariant(const QMap<QString, QVariant>& baseMap) const {
        QMap<QString, QVariant> extendedMap = baseMap;
        // EntranceModel::ChartType currentType = model->getChartType();
        // qDebug() << "Current Chart Type:" << static_cast<int>(currentType);

        extendedMap.insert("TypeChart", static_cast<int>(model->getChartType()));
        return QVariant(extendedMap);
    }

    EntranceModel *model;
    EntranceModel::ChartType typeChart;
};

#endif // ENTRANCEVIEWMODEL_HPP
